package com.vanzo.contact;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.vanzo.bean.Contact;
import com.vanzo.bean.ContactDaoUtil;
import com.vanzo.talkie2.R;

/**
 * Created by tisen on 2018/1/18.
 */

public class ContactAddActivity extends Activity {

    private String action;
    private Contact contact;

    private EditText name;
    private EditText unit;
    private EditText phone;
    private EditText beidou;

    private Button save;
    private ContactDaoUtil contactDaoUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_edit);
        init();
    }

    private void init() {
        contactDaoUtil = new ContactDaoUtil(this);
        action = getIntent().getAction();
        Long id = getIntent().getLongExtra("id", 0);
        contact = contactDaoUtil.queryContactById(id);
        name = (EditText) findViewById(R.id.contact_name_edit);
        unit = (EditText) findViewById(R.id.contact_unit_edit);
        phone = (EditText) findViewById(R.id.contact_phone_edit);
        beidou = (EditText) findViewById(R.id.contact_beidou_edit);
        save = (Button) findViewById(R.id.contact_save_button);

        switch (action) {
            case ContactsActivity.ACTION_ADD:
                break;
            case ContactsActivity.ACTION_VIEW:
                name.setText(contact.getName());
                name.setEnabled(false);
                unit.setText(contact.getUnit());
                unit.setEnabled(false);
                phone.setText(contact.getPhone());
                phone.setEnabled(false);
                beidou.setText(contact.getBeidou());
                beidou.setEnabled(false);
                save.setVisibility(View.INVISIBLE);
                break;
            case ContactsActivity.ACTION_EDIT:
                name.setText(contact.getName());
                unit.setText(contact.getUnit());
                phone.setText(contact.getPhone());
                beidou.setText(contact.getBeidou());
                break;
        }

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name.getText().toString().trim().isEmpty()) {
                    Toast.makeText(ContactAddActivity.this, "姓名不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (contact == null) {
                        contact = new Contact();
                    }
                    contact.setName(name.getText().toString());
                    contact.setUnit(unit.getText().toString());
                    contact.setPhone(phone.getText().toString());
                    contact.setBeidou(beidou.getText().toString());
                    if (contact.getId() == null) {
                        contactDaoUtil.insertContact(contact);
                    } else {
                        contactDaoUtil.updateContact(contact);
                    }
                    setResult(ContactsActivity.CONTACTS_CHANGE);
                    finish();
                }
            }
        });
    }
}
